<?php
namespace Admin\Controller;

use Admin\Controller\GlobalController;

class NewsController extends GlobalController
{
    public function change_order() {
        if (!IS_POST) exit();
        $news_id = I('post.news_id', 0, 'intval');
        $order_by = I('post.order_by', 0, 'intval');
        $newsModel = M('news');
        $newsModel->where(array('news_id' => $news_id))->setField('order_by', $order_by);
        $this->ajaxReturn('', 'success', 'TIP_SUCCESS');
    }

    public function getlist()
    {
        $pageSize = 20;
        $where = array();
        $newsModel = M('news');
        $count = $newsModel->where($where)->count();
        $pageObj = new \Admin\Util\Page($count, $pageSize);
        $list = $newsModel->where($where)->order('date_ymd desc, order_by asc')->limit($pageObj->firstRow . ',' . $pageObj->listRows)->select();
        $commentModel = M('comment');
        foreach ($list as $key => $row) {
            $map = array('type' => 2, 'id' => $row['news_id'], 'is_deleted' => '0');
            $commentCount = $commentModel->where($map)->count();
            $list[$key]['commentCount'] = $commentCount;
        }
        $this->assign('auto_publish_hours', $this->auto_publish_hours);
        $this->assign('list', $list);
        $this->assign('page', $pageObj->show());
        $this->display();
    }

    public function crontablist()
    {
        $cModel = M('crontab');
        $pageSize = 10;
        $where = array('identifier' => 'push_broadcast');
        $count = $cModel->where($where)->count();
        $pageObj = new \Admin\Util\Page($count, $pageSize);
        $list = $cModel->where($where)->order('auto_publish_time desc')->limit($pageObj->firstRow . ',' . $pageObj->listRows)->select();
        $this->assign('list', $list);
        $this->assign('page', $pageObj->show());
        $this->display();
    }

    //添加或者修改
    public function save()
    {
        $newsModel = M('news');
        if (IS_POST) {
            $news_id = I('post.news_id', 0, 'intval');
            if (I('post.auto_publish_status', 0, 'intval') == 1) {
                $date_str = I('post.auto_publish_time', '', 'trim') . ' ' . I('post.auto_publish_hour', '', 'trim');
                $data['auto_publish_time'] = strtotime($date_str);
            } else {
                $data['auto_publish_time'] = 0;
            }
            $data['duration'] = I('post.duration', 0, 'intval');
            $data['date_ymd'] = I('post.date_ymd', 0, 'strtotime');
            $data['date_ymd'] = date('Ymd', $data['date_ymd']);
            $data['nc_id'] = I('post.nc_id', 0, 'intval');
            $data['news_img'] = I('post.news_img', '', 'trim');
            $data['share_img'] = I('post.share_img', '', 'trim');
            $data['hide_bottom_bar'] = I('post.hide_bottom_bar', 0, 'intval');
            $data['redirect_url'] = I('post.redirect_url', '', 'trim');
            $data['news_title'] = I('post.news_title', '', 'trim');
            $data['type'] = I('post.type', 'video', 'trim');
            $data['content'] = I('post.content', '', 'trim');
            $data['video_bg'] = I('post.video_bg', '', 'trim');
            $data['sound_bg'] = I('post.sound_bg', '', 'trim');
            $data['sound_img'] = I('post.sound_img', '', 'trim');
            if ($data['type'] != 'imgs') {
                $data['content'] = current(explode(',', $data['content']));
            }
            $data['description'] = I('post.description', '', 'trim');
            $data['description'] = str_replace("<p><br/></p>", '', $data['description']);
            if ($news_id) {
                $newsModel->where(array('news_id' => $news_id))->save($data);
            } else {
                $data['add_time'] = time();
                $newsModel->add($data);
                $news_id = $newsModel->getLastInsID();
            }
            $tplArr = C('TMPL_PARSE_STRING');
            if (I('post.type') == 'web' && empty($data['redirect_url'])) {
                $newsModel->where(array('news_id' => $news_id))->setField('redirect_url', $tplArr['__URL_ROOT__'] . '/mobile/description/news_id/' . num_encode($news_id));
            }
            $this->success('操作成功', array(array('返回列表', U('admin/news/getlist'))));
            exit();
        }
        $news_id = I('get.news_id', 0, 'intval');
        $info = $newsModel->where(array('news_id' => $news_id))->find();
        if ($info) {
            $info['date_ymd'] = date('Y-m-d', strtotime($info['date_ymd']));
        } else {
            $info = array('auto_publish_time' => 0);
        }

        $ncModel = M('news_cate');
        $ncList = $ncModel->select();
        $this->assign('ncList', $ncList);

        $this->assign('news_id', $news_id);
        $this->assign('auto_publish_hours', $this->auto_publish_hours);
        $this->assign('info', $info);
        $this->display();
    }

    public function publish()
    {
        if (IS_POST) {
            $news_id = I('post.news_id', '', 'intval');
            $newsModel = M('news');
            $newsModel->where(array('news_id' => $news_id))->setField('status', array('exp', '1 - status'));
            $this->ajaxReturn('', 'success', 'TIP_SUCCESS');
        }
    }

    public function del()
    {
        $news_id = I('post.news_id', 0, 'intval');
        $newsModel = M('news');
        $bool = $newsModel->where(array('news_id' => $news_id))->delete();
        $this->ajaxReturn('', 'success', 'TIP_SUCCESS');
    }
}